<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*-----------------------------------------------------------------------------------------------    
        
    -----------------------------------------------------------------------------------------------*/
class Shopping_cart_block extends Front_Controller{

    function Shopping_cart_block(){
        parent::Front_Controller();
    }
    
    function index(){
        $CI =&get_instance();    
		$act = $CI->uri->segment(2);
		$id = (int)($CI->uri->segment(3));
		/*    
		if(isset($_GET['id'])){
			$id = $_GET['id'];
		}*/
		///ds mat hang : 1,2,3,4....
		//$_SESSION['dsmathang'] = $_SESSION['dsmathang'].",".$id."  ";
		//$_SESSION['soluong'] = $_SESSION['soluong'].",1";
		//Khoi gian cho gia tri ban dau la 1 mang rong
		$shopping_cart = array();
		$so_luong = array();
		//Kiem tra trong gio hang da co mat hang nao chua
		//Neu co thi thuc hien nhu sau
		//unset($_SESSION['dsmathang']);unset($_SESSION['soluong']);exit();
		if($id&&$act =='SP'){			
			if(($CI->session->userdata('dsmathang')) && ($CI->session->userdata('soluong')))
			{
				//Tach bien SESSION thang mang
				$shopping_cart =split(",", $CI->session->userdata('dsmathang'));
				$so_luong =split(",", $CI->session->userdata('soluong'));
				//Bat co flag = 0;
				$flag = 0;
				//Duyet mang tu phan tu dau tien den cuoi mang	
				for($i=0;$i<count($shopping_cart);$i++)
				{	//Neu mot mang duoc chon da co trong gio hang
					if($shopping_cart[$i]==$id) 
					{
						$so_luong[$i]++;
						//Bat co flag = 1;
						$flag = 1;
						break;
					}
				}	
				
				//Neu nhu mang da duyet het, tuc la san pham nay chua co trong gio hang
				if($flag == 0){
					$shopping_cart[count($shopping_cart)] = $id;
					$so_luong[count($shopping_cart)] = 1;
				}
				//Ket qua cuoi cung tra ve SESSION
				$CI->session->set_userdata('dsmathang',join(",", $shopping_cart));
				$CI->session->set_userdata('soluong',join(",", $so_luong));
				
			
			}
			
			//Nguoc lai neu khong co mat hang nao 
			else{				
				$CI->session->set_userdata('dsmathang',$id);
				$CI->session->set_userdata('soluong',"1");
			
			}
		}
		if($id&&$act =='XOA'){
			$shopping_cart =split(",", $CI->session->userdata('dsmathang'));
			$so_luong =split(",", $CI->session->userdata('soluong'));
			
			for($i=0;$i<count($shopping_cart);$i++)
			{	//Neu mat mang duoc chon da co trong gio hang
				if($shopping_cart[$i]==$id) 
				{
					for($j=$i+1;$j<count($shopping_cart);$j++)
					{
						$shopping_cart[$i] = $shopping_cart[$j];
						$so_luong[$i] = $so_luong[$j];
						 
					}
					array_pop($shopping_cart);
					array_pop($so_luong);
				}
				
			}
			$CI->session->set_userdata('dsmathang',join(",", $shopping_cart));
			$CI->session->set_userdata('soluong',join(",", $so_luong));
		}
		
		$update ="";
		if(isset($_POST['update'])){
			$update = $_POST['update'];
		}
		if($update){
			//print_r($_POST['cart_quantity']);
			//print_r($_POST['products_id']);
			$CI->session->set_userdata('dsmathang',join(",", $_POST['products_id']));
			$CI->session->set_userdata('soluong',join(",", $_POST['cart_quantity']));
			
		}
		$shopping_cart =split(",", $CI->session->userdata('dsmathang'));
		$so_luong =split(",", $CI->session->userdata('soluong'));
		
		for($i=0;$i<count($shopping_cart);$i++)
		{	//Neu mat mang duoc chon da co trong gio hang
			if($so_luong[$i]==""||$so_luong[$i]==0) 
			{
				for($j=$i+1;$j<count($shopping_cart);$j++)
				{
					$shopping_cart[$i] = $shopping_cart[$j];
					$so_luong[$i] = $so_luong[$j];
					 
				}
				array_pop($shopping_cart);
				array_pop($so_luong);
			}
			
		}
		$CI->session->set_userdata('dsmathang',join(",", $shopping_cart));
		$CI->session->set_userdata('soluong',join(",", $so_luong));
		
		//print_r($CI->session->userdata('dsmathang'));
		//echo "ds mat hang:" .$_SESSION['dsmathang'];
		//echo "<br>";
		//echo "ds so luong:" .$_SESSION['soluong'];
		
		//$sql="select * from mathang where id in (".$_SESSION['dsmathang'].")" ;
		//$result = mysql_query($sql);
		if($CI->session->userdata('dsmathang')){
			$CI->db->where(PRODUCTS_PRODUCT_ID.' in ('.$CI->session->userdata('dsmathang').')', NULL, FALSE);
			$q = $CI->db->get(PRODUCTS);
			$products = $q->result();
		}
		else $products = array();

		$this->add('products',$products);
		$this->add('shopping_cart',$shopping_cart);
		$this->add('so_luong',$so_luong);
        $this->view(get_class($this));
    }

}